package com.kinon.bs_app.activities;

import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.Toast;

import com.kinon.bs_app.R;
import com.kinon.bs_app.fragments.WodeFragment;
import com.kinon.bs_app.fragments.XuanbiFragment;
import com.kinon.bs_app.utils.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private FrameLayout fl_frame = null;
    private List<Fragment> fragments = null;
    private FragmentManager fragmentManager = null;

    private RadioButton rb_xuanbi = null;
    private RadioButton rb_wode = null;

    private int currentItem = 0;
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        iniListener();
    }

    private void initView() {
        rb_xuanbi = findViewById(R.id.rb_xuanbi);
        rb_wode = findViewById(R.id.rb_wode);

        rb_xuanbi.setCompoundDrawables(null,
                stardardImg(R.drawable.img_select_xuanbi),null,null);
        rb_wode.setCompoundDrawables(null,
                stardardImg(R.drawable.img_select_wode), null,null);

        fragments = new ArrayList<>();
        addFragments();

    }

    private void iniListener() {
        rb_xuanbi.setOnClickListener(this);
        rb_wode.setOnClickListener(this);
    }

    private void addFragments() {
        fragments.add(new XuanbiFragment());
        fragments.add(new WodeFragment());
        showFirstFragment();
    }

    private void showFirstFragment() {
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fl_frame, fragments.get(currentItem));
        fragmentTransaction.show(fragments.get(currentItem));
        fragmentTransaction.commit();
    }

    private void showFragment(int position) {
        if (currentItem != position) {
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            //隐藏之间显示的fragment
            fragmentTransaction.hide(fragments.get(currentItem));
            //判断fragment是否加载过，
            if (!fragments.get(position).isAdded()) {
                fragmentTransaction.add(R.id.fl_frame, fragments.get(position));
            }
            fragmentTransaction.show(fragments.get(position));
            fragmentTransaction.commit();
            currentItem = position;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_xuanbi:
                showFragment(0);
                break;
            case R.id.rb_wode:
                showFragment(1);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis()-exitTime) > 2000) {
                showToast("再按一次退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public Drawable stardardImg(int position) {
        Drawable drawable = getResources().getDrawable(position);
        drawable.setBounds(0, 0, 50, 50);
        return drawable;
    }
}
